package org.zty.util;


import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;



public class UploadUtil {


	public static Map<String, Object> initUpload(HttpServletRequest request) throws Exception {

		
		DiskFileItemFactory factory = new DiskFileItemFactory ();
		// 设置缓冲区大小，这里是4kb
		factory.setSizeThreshold(4096);

		/**创建一个新的文件上传操作*/
		ServletFileUpload upload = new ServletFileUpload(factory);

		/**设置能上传的最大文件的大小*/
		upload.setSizeMax(2000*1024*1024);
		/**Parse the request*/
		List fileItems = upload.parseRequest(request);
		//开始读取上传信息
		Iterator iter = fileItems.iterator();
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		// 依次处理每个上传的文件
		while(iter.hasNext()) {
			FileItem item = (FileItem) iter.next();
			// 忽略其他不是文件域的所有表单信息
			if (item.isFormField()) {
				paramMap.put(item.getFieldName(), new String(item.getString().getBytes("iso8859-1"), "utf-8"));
	            }
		  	else {
		  		
		  		paramMap.put(item.getFieldName(), item);
			}
		}
		return paramMap;
	}
}
